var Info = app.getUserGlobalInfo();
var table_tby = document.getElementById("table-tby");
$.ajax({
  url: app.ServerUrl + '\n' +
    '/v1/repository/getEliminateRequests',
  type: "get",
  dataType: "json",
  contentType: "application/json;charset=UTF-8",
  async: true,
  headers: {
    "Authorization": "460bcdb0-b326-4d6d-ac99-232c83ba6d80"
  },
  data: {
    roleId: Info.roleId
  },
  success: function (data) {
    // console.log(data)
    var result = data.objs[0];
    localStorage.setItem("singleInfo", JSON.stringify(result));
    var html = '';
    for (let i = 0; i < result.length; i++) {
      var singleInfo = result[i];
      if (singleInfo.status == 0) {
        var singleInfo_str = JSON.stringify(singleInfo);
        var inTime = app.getFormatDate(singleInfo.requestTime);
        html +=
          `<tr> 
                <td> ${singleInfo.num != null? singleInfo.num:singleInfo.serialNum}  </td>
                <td> ${singleInfo.categoryName} </td> 
                <td>  ${singleInfo.typicalName}  </td> 
                <td>  ${singleInfo.brand}  </td> 
                <td>  ${singleInfo.typicalModel} </td> 
                <td>  ${inTime}  </td>
                <td><span style="color: red;">待审核</span></td>
                <td> 
                <button type="button" class="layui-btn layui-btn-sm" onclick="ConfirmItem(this,'${singleInfo.saleId}','${singleInfo.stuffId}')"> 
                通过 
                </button> 
                <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" onclick="Reject('${singleInfo.saleId}')"> 
                驳回  
                </button>
                <button class="layui-btn layui-btn-warm layui-btn-xs"  onclick="xadmin.open('申请者详细信息','detail.html?x='
                +'${Info.roleId}',460,280)" >申请者详情</button>
                </td>
            </tr>`
      } else if (singleInfo.status == 1) {
        var singleInfo_str = JSON.stringify(singleInfo);
        var inTime = app.getFormatDate(singleInfo.requestTime);
        html +=
          `<tr> 
                <td> ${singleInfo.num !=null? singleInfo.num:singleInfo.serialNum}  </td>
                <td> ${singleInfo.categoryName} </td> 
                <td>  ${singleInfo.typicalName}  </td> 
                <td>  ${singleInfo.brand}  </td> 
                <td>  ${singleInfo.typicalModel} </td> 
                <td>  ${inTime}  </td>
                <td><span style="color: red;">已通过</span></td>
                <td>
                <button class="layui-btn layui-btn-warm layui-btn-xs"  onclick="xadmin.open('申请者详细信息','detail.html?x='
                +'${Info.roleId}',460,280)" >申请者详情</button>
                </td>
                </tr>`
      } else if (singleInfo.status == 2) {
        var singleInfo_str = JSON.stringify(singleInfo);
        var inTime = app.getFormatDate(singleInfo.requestTime);
        html +=
          `<tr> 
                <td> ${singleInfo.num !=null? singleInfo.num:singleInfo.serialNum}  </td>
                <td> ${singleInfo.categoryName} </td> 
                <td>  ${singleInfo.typicalName}  </td> 
                <td>  ${singleInfo.brand}  </td> 
                <td>  ${singleInfo.typicalModel} </td> 
                <td>  ${inTime}  </td>
                <td><span style="color: red;">已驳回</span></td>
                <td> 
                <button class="layui-btn layui-btn-warm layui-btn-xs"  onclick="xadmin.open('申请者详细信息','detail.html?x='
                +'${Info.roleId}',460,280)" > 申请者详情</button>
                </td>
                </tr>`
      }
    }
    table_tby.innerHTML = html;
  },
  error: function (e) {
    console.log(e.statusText)
  }
})

// 通过销库请求
function ConfirmItem(e, saleId, stuffId) {
  layer.open({
    title: '提示信息',
    content: '确认要通过这条申请吗？',
    btn: ['确定', '取消'],
    yes: function (index) {
      var arr = [];
      arr.push(saleId)
      // console.log(stuffId);
      if (stuffId != null) {
        arr.push(stuffId)
      }
      // console.log(JSON.stringify(arr));
      $.ajax({
        url: app.ServerUrl + '\n' +
          'v1/repository/eliminateInventory',
        type: "post",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        async: true,
        headers: {
          "Authorization": "460bcdb0-b326-4d6d-ac99-232c83ba6d80"
        },
        data: JSON.stringify(
          arr
        ),
        success: function (data) {
          // console.log(data)
          if (data.resultStatus == 1) {
            layer.msg('销库申请已通过!', {
              icon: 1,
              time: 1000
            }, function () {
              window.location.reload();
            });
          } else {
            layer.msg('操作失败！', function () {
              window.location.reload();
            });
          }
        },
        error: function (e) {
          layer.msg('操作失败！', function () {
            window.location.reload();
          });
        }
      })
    },
    cancel: function () {}
  })
}

// 驳回销库请求
function Reject(saleId) {
  layer.open({
    title: '提示信息',
    content: '确认要驳回这条申请吗？',
    btn: ['确定', '取消'],
    yes: function (index) {
      $.ajax({
        url: app.ServerUrl + '\n' +
          'v1/repository/rejectEliminateRequest',
        type: "get",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        async: true,
        headers: {
          "Authorization": "460bcdb0-b326-4d6d-ac99-232c83ba6d80"
        },
        data: {
          saleId: saleId
        },
        success: function (data) {
          // console.log(data)
          if (data.resultStatus == 1) {
            layer.msg('销库申请已驳回!', {
              icon: 1,
              time: 1000
            }, function () {
              window.location.reload();
            });
          } else {
            layer.msg('操作失败！', function () {
              window.location.reload();
            });
          }
        },
        error: function (e) {
          layer.msg('操作失败！', function () {
            window.location.reload();
          });
        }
      })
    },
    cancel: function () {}
  })
}
